﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ResumeHelper
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (CheckProcessAll() == false)
            {
                return;
            }

            if (WebSiteProcessWorker.InitUser(txtEmail.Text.Trim(), txtPWD.Text.Trim()) == false)
            {
                Util.ShowInfoMessage("您的电子邮件没有注册或密码不正确！");
                Util.HighLightControl(txtEmail);
                return;
            }

            if (WebSiteProcessWorker.InitWBPNameList() == 0)
            {
                Util.ShowInfoMessage("没有正确的更新网站列表。这可能是服务器忙引起的。请您稍后再试。");
                Util.HighLightControl(txtEmail);
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }


        private void frmLogin_Load(object sender, EventArgs e)
        {
            txtEmail.Text = "uzdust@gmail.com";
            txtPWD.Text = "0123456789";
        }

        private bool CheckProcessAll()
        {
            if (CheckProcess(txtEmail) == false)
            {
                return false;
            }
            if (CheckProcess(txtPWD) == false)
            {
                return false;
            }

            return true;
        }

        private bool CheckProcess(Control con)
        {
            if (con.Name == txtEmail.Name)
            {
                if (txtEmail.Text.Trim()=="")
                {
                    Util.ShowInfoMessage("请输入邮件地址！");
                    Util.HighLightControl(con);
                    return false;
                }

                if (Util.isEmail(txtEmail.Text) == false)
                {
                    Util.ShowInfoMessage("请输入正确的邮件地址！");
                    Util.HighLightControl(con);
                    return false;
                }
            }
            if (con.Name == txtPWD.Name)
            {
                if (txtPWD.Text.Trim() == "")
                {
                    Util.ShowInfoMessage("请输入密码！");
                    Util.HighLightControl(con);
                    return false;
                }

                if (Util.IsPassword(txtPWD.Text) == false)
                {
                    Util.ShowInfoMessage("请输入正确的密码！");
                    Util.HighLightControl(con);
                    return false;
                }
            }

            return true;
        }

        public bool IsOK { get; set; }

        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            Console.WriteLine(e.Url.ToString());
            if (e.Url.ToString().Equals(PublicObject.WEBURL_LOGIN_STARTUP_ADDRESS) == true)
            {
                IsOK = true;
            }
        }
    }
}
